package com.lyf.scm.admin.domain.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.remote.allocationservice.facade.SalesPlanOtherDifferinFacade;
import com.lyf.scm.admin.domain.remote.basedata.facade.BaseDataFacade;
import com.lyf.scm.admin.domain.remote.item.facade.ItemCoreFacade;
import com.lyf.scm.admin.domain.service.SalesPlanOtherDifferinService;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.enums.SalesPlanOtherDifferinEnum;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.util.date.MyDateUtil;
import com.rome.arch.core.exception.RomeException;
import com.rome.user.common.utils.UserContext;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * description:
 * 其他渠道计划_目标与销售计划差异serviceImpl
 *
 * @author: huangtao
 * @data: 2020/7/31
 */
@Slf4j
@Service
public class SalesPlanOtherDifferinServiceImpl implements SalesPlanOtherDifferinService {

    @Resource
    private SalesPlanOtherDifferinFacade salesPlanOtherDifferinFacade;

    @Resource
    private BaseDataFacade baseDataFacade;

    @Resource
    private ItemCoreFacade itemCoreFacade;

    /**
     * 按条件查询其他渠道计划_目标与销售计划差异(周)
     * @param salesPlanOtherDTO
     * @return
     */
    @Override
    public PlanResponseDTO<SalesPlanOtherDifferinInfoDTO> listWeekBySelective(SalesPlanOtherDTO salesPlanOtherDTO) {
        //获取当前角色id(查询角色权限用)
        salesPlanOtherDTO.setCreator(UserContext.getUserId());
        //根据日期获取间隔日期集合
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(salesPlanOtherDTO.getStartTime(), CommonConstants.WEEK_SIZE);
        salesPlanOtherDTO.setWeekDateList(weekDates);
        //处理查询条件
        // List<SalesPlanSelectDTO> channelCodeList = baseDataFacade.getSubChannelCode(salesPlanOtherDTO.getChannelCodeList());
        // List<SalesPlanSelectDTO> areaCodeList = baseDataFacade.getSubAreaCode(salesPlanOtherDTO.getAreaCodeList());
        // List<SalesPlanSelectDTO> categoryCodeList = itemCoreFacade.getSubcategoryCode(salesPlanOtherDTO.getCategoryCodeList());
        // BaseSearchUtil.setSearchFields(salesPlanOtherDTO, channelCodeList, categoryCodeList, areaCodeList);
        PageInfo<SalesPlanOtherDifferinInfoDTO> pageInfo = salesPlanOtherDifferinFacade.listWeekBySelective(salesPlanOtherDTO);
        PlanResponseDTO<SalesPlanOtherDifferinInfoDTO> planResponse = new PlanResponseDTO<>();
        planResponse.setInfoList(pageInfo.getList());
        //格式化日期格式
        planResponse.setDateList(MyDateUtil.processCyclesDate(weekDates, "M.dd"));
        planResponse.setTotal(pageInfo.getTotal());
        return planResponse;
    }

    /**
     * 图表数据接口
     *
     * @param salesPlanOtherDTO
     * @return
     */
    @Override
    public EChartsDTO eChartsData(SalesPlanOtherDTO salesPlanOtherDTO) {
        //获取当前角色id(查询角色权限用)
        salesPlanOtherDTO.setCreator(UserContext.getUserId());
        EChartsDTO eChartsDTO = new EChartsDTO();
        //如果开始时间为空
        if (ObjectUtil.isEmpty(salesPlanOtherDTO.getStartTime())) {
            salesPlanOtherDTO.setStartTime(DateUtil.beginOfWeek(new Date()));
        } else {
            salesPlanOtherDTO.setStartTime(DateUtil.beginOfWeek(salesPlanOtherDTO.getStartTime()));
        }
        //如果结束时间为空
        if (ObjectUtil.isEmpty(salesPlanOtherDTO.getEndTime())) {
            salesPlanOtherDTO.setEndTime(DateUtil.beginOfWeek(DateUtil.offsetWeek(salesPlanOtherDTO.getStartTime(), CommonConstants.WEEK_TARGET_SIZE)));
        } else {
            salesPlanOtherDTO.setEndTime(DateUtil.beginOfWeek(DateUtil.offsetWeek(salesPlanOtherDTO.getEndTime(), CommonConstants.ONE)));
        }
        List<SalesPlanOtherDifferinInfoDTO> salesPlanOtherDifferinInfoDTOS = salesPlanOtherDifferinFacade.listAllByCycles(salesPlanOtherDTO);
        //计算出开始时间和结束时间之间的周数
        List<Map<String, Date>> weekDates = MyDateUtil.getWeekDates(salesPlanOtherDTO.getStartTime(), (int) DateUtil.betweenWeek(salesPlanOtherDTO.getStartTime(), salesPlanOtherDTO.getEndTime(), false));
        salesPlanOtherDTO.setWeekDateList(weekDates);
        //返回对应的key和label值
        List<EChartsLabelDTO> labelData = new ArrayList<>();
        SalesPlanOtherDifferinEnum[] values = SalesPlanOtherDifferinEnum.values();
        for (SalesPlanOtherDifferinEnum value : values) {
            EChartsLabelDTO eChartsLabelDTO = new EChartsLabelDTO();
            eChartsLabelDTO.setKey(value.getCode());
            eChartsLabelDTO.setLabel(value.getDesc());
            labelData.add(eChartsLabelDTO);
        }
        Map<String, List<BigDecimal>> value = new HashMap<>();
        if(CollectionUtils.isNotEmpty(salesPlanOtherDifferinInfoDTOS)){
            salesPlanOtherDifferinInfoDTOS.forEach(item->{
                //value.put(SalesPlanOtherDifferinEnum.SALES_TARGET_AMOUNT.getCode(), item.getSalesTargetAmounts());
                value.put(SalesPlanOtherDifferinEnum.SALES_CONFIRM_AMOUNT.getCode(), item.getSalesConfirmAmounts());
                value.put(SalesPlanOtherDifferinEnum.ACTUAL_SALES_AMOUNT.getCode(), item.getActualSalesAmounts());
                value.put(SalesPlanOtherDifferinEnum.ACTUAL_SALES_QTY.getCode(), item.getActualSalesQtys());
                value.put(SalesPlanOtherDifferinEnum.SALES_CONFIRM_QTY.getCode(), item.getSalesConfirmQtys());
                //value.put(SalesPlanOtherDifferinEnum.TARGET_ACRATE_AMOUNT.getCode(), item.getTargetAcrateAmounts());
                value.put(SalesPlanOtherDifferinEnum.ACTUAL_RECEIVED_QTY.getCode(),item.getActualReceivedQtys());
            });
        }
        List<String> strings = MyDateUtil.processCyclesDate(weekDates, "M.dd");
        eChartsDTO.setValue(value);
        eChartsDTO.setDateList(strings);
        eChartsDTO.setLabelData(labelData);
        return eChartsDTO;

    }

    /**
     *
     * @param excelExportDTO
     * @param type
     * @return
     */
    @Override
    public Response export(ExcelExportDTO excelExportDTO, Integer type) {
        if (ObjectUtil.isEmpty(type)) {
            throw new RomeException(ResponseMsg.FAIL_2005.getCode(), ResponseMsg.FAIL_2005.getMsg());
        }
        SalesPlanOtherDTO salesPlanOtherDTO=new SalesPlanOtherDTO();
        //获取当前角色id(查询角色权限用)
        salesPlanOtherDTO.setCreator(UserContext.getUserId());
        salesPlanOtherDTO.setLayeredCodeList(excelExportDTO.getLayeredCodeList());
        salesPlanOtherDTO.setChannelCodeList(excelExportDTO.getChannelCodeList());
        salesPlanOtherDTO.setAreaCodeList(excelExportDTO.getAreaCodeList());
        salesPlanOtherDTO.setCategoryCodeList(excelExportDTO.getCategoryCodeList());
        salesPlanOtherDTO.setPageNum(excelExportDTO.getPageNum());
        salesPlanOtherDTO.setPageSize(excelExportDTO.getPageSize());
        if(ObjectUtil.isNotEmpty(excelExportDTO.getBaseSearchS())){
            List<SalesPlanOtherDTO> salesPlanOtherDTOS=new ArrayList<>();
            excelExportDTO.getBaseSearchS().forEach(item->{
                SalesPlanOtherDTO dto=new SalesPlanOtherDTO();
                dto.setChannelCode(item.getChannelCode());
                dto.setSubChannelCode(item.getSubChannelCode());
                dto.setProvinceCode(item.getProvinceCode());
                dto.setCityCode(item.getCityCode());
                dto.setCountyCode(item.getCountyCode());
                dto.setStoreCode(item.getStoreCode());
                dto.setPriCategoryCode(item.getPriCategoryCode());
                dto.setSecCategoryCode(item.getSecCategoryCode());
                dto.setTerCategoryCode(item.getTerCategoryCode());
                dto.setSkuCode(item.getSkuCode());
                salesPlanOtherDTOS.add(dto);
            });
            salesPlanOtherDTO.setSalesPlanOtherList(salesPlanOtherDTOS);
        }
        //处理开始时间
        salesPlanOtherDTO.setStartTime(excelExportDTO.getStartTime());
        List<Map<String, Date>> weekDate = null;
        //选择时间范围导出
        if (ObjectUtil.isNotNull(excelExportDTO.getEndTime())) {
            weekDate = MyDateUtil.getWeekDates(salesPlanOtherDTO.getStartTime(),excelExportDTO.getEndTime());
        } else {
            //根据日期获取间隔日期集合,没有结束时间，默认14周
            weekDate = MyDateUtil.getWeekDates(salesPlanOtherDTO.getStartTime(), CommonConstants.WEEK_SIZE);
        }
        salesPlanOtherDTO.setWeekDateList(weekDate);
        //计算出默认的结束时间
        salesPlanOtherDTO.setEndTime(MyDateUtil.getLastDate(null, weekDate));
        salesPlanOtherDTO.setUnitType(excelExportDTO.getUnitType());
        //获取三个查询条件的实际集合
        // List<SalesPlanSelectDTO> channelCodeList = baseDataFacade.getSubChannelCode(excelExportDTO.getChannelCodeList());
        // List<SalesPlanSelectDTO> categoryCodeList = itemCoreFacade.getSubcategoryCode(excelExportDTO.getCategoryCodeList());
        // List<SalesPlanSelectDTO> areaCodeList = baseDataFacade.getSubAreaCode(excelExportDTO.getAreaCodeList());
        // BaseSearchUtil.setSearchFields(salesPlanOtherDTO, channelCodeList, categoryCodeList, areaCodeList);
        return salesPlanOtherDifferinFacade.export(salesPlanOtherDTO,type);
    }


}
